package com.llh.cheap.configs;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @author David
 */
@Configuration
@ComponentScan(basePackages = "com.qxt.sms")
@PropertySource(value = "classpath:app.properties", encoding = "UTF-8")
public class SmsConfig {

    @Bean
    public SmsClient smsClient(
            @Value("${sms.tencent.secret_id}") String secretId,
            @Value("${sms.tencent.secret_key}") String secretKey
    ) {
        Credential cred = new Credential(secretId, secretKey);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("POST");
        httpProfile.setConnTimeout(60);
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);

        return new SmsClient(cred, "ap-guangzhou", clientProfile);
    }

    @Bean
    public WxCpService wxCpService(@Value("${wx.cp.corp_id}") String corpId, @Value("${wx.cp.corp_secret}") String corpSecret, @Value("${wx.cp.agent_id}") int agentId) {
        WxCpService wxCpService = new WxCpServiceImpl();
        WxCpDefaultConfigImpl cpConfigStorage = new WxCpDefaultConfigImpl();
        cpConfigStorage.setCorpId(corpId);
        cpConfigStorage.setCorpSecret(corpSecret);
        cpConfigStorage.setAgentId(agentId);
        wxCpService.setWxCpConfigStorage(cpConfigStorage);
        return wxCpService;
    }
}
